//
//  LedMenuTalbeViewCell.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/12/28.
//  Copyright © 2021 moon. All rights reserved.
//

#import "LedMenuTalbeViewCell.h"
#import "Menu.h"

@interface LedMenuCollectionViewCell : MKUICollectionViewCellBase
@property(nonatomic,strong) UILabel *menuLabel;
@end
@implementation LedMenuCollectionViewCell
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.menuLabel = [[UILabel alloc] init];
        self.menuLabel.font = [UIFont systemFontOfSize:12];
        self.menuLabel.numberOfLines = 2;
        self.menuLabel.textColor = [UIColor blackColor];
        [self.contentView addSubview:self.menuLabel];
        self.contentView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.contentView.layer.borderWidth = 1;
    }
    return self;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.menuLabel.frame = bounds;
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    Menu *menu = self.collectionCellModel.modelValue;
    self.menuLabel.text = menu.title;
}
@end

@interface LedMenuTalbeViewCell()<MKUILedBannerVerticalViewDelegate>
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) MKUILedBannerVerticalView *bannerView;
@end

@implementation LedMenuTalbeViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.bgView = [[UIView alloc] init];
        self.bgView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.bgView.layer.borderWidth = 1;
        self.bgView.layer.cornerRadius = 5;
        self.bgView.clipsToBounds = YES;
        [self.contentView addSubview:self.bgView];
        
        self.bannerView = [[MKUILedBannerVerticalView alloc] init];
        self.bannerView.delegate = self;
        self.bannerView.defaultContentCellClass = [LedMenuCollectionViewCell class];
        [self.contentView addSubview:self.bannerView];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.bgView.frame = bounds;
    self.bannerView.frame = bounds;
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    NSArray<Menu *> *menus = self.cellModel.modelValue;
    self.bannerView.contents = menus;
    [self.bannerView reloadData];
}
- (void)tableView:(UITableView *)tableView willDisplayCellModel:(__kindof MKUITableViewCellModel *)cellModel{
    
    if(self.isCellModelChanged){
        [self layoutIfNeeded];
        NSInteger currentIndex = [self.cellModel mk_integerForKeyPath:@"currentIndex" otherwise:0];
        self.bannerView.currentIndex = currentIndex;
    }
    BOOL autoScroll = [self.cellModel mk_boolForKeyPath:@"autoScroll" otherwise:YES];
    if(autoScroll){
        [self.bannerView startAutoScrollingWithDistance:1 duration:2];
    }
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCellModel:(__kindof MKUITableViewCellModel *)cellModel{
    [self.bannerView stopAutoScrolling];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeMake(size.width, 50);
}
- (void)ledBannerVerticalView:(MKUILedBannerVerticalView *)view didScrollToBannerWithIndex:(NSInteger)currentIndex{
    self.cellModel[@"currentIndex"] = @(currentIndex);
}
- (void)ledBannerVerticalView:(MKUILedBannerVerticalView *)view didClickBannerWithIndex:(NSInteger)currentIndex bannerContent:(NSObject *)content{
    BOOL autoScroll = [self.cellModel mk_boolForKeyPath:@"autoScroll" otherwise:YES];
    if(autoScroll){
        if(self.bannerView.isAutoScrolling){
            [self.bannerView stopAutoScrolling];
        }else{
            [self.bannerView startAutoScrollingWithDistance:1 duration:2];
        }
    }else{
        [self.bannerView setCurrentIndexWithDistance:1 animated:YES];
    }
}
@end
